﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Facebook;
using System.Diagnostics;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace FBEventMaker
{
    public class WallPostDataResources
    {
        /// <summary>
        /// Used fql multi query to select the name of the user where the uid is equal to actor_id in stream
        /// </summary>
        private static string[] m_FqlQuery = {"SELECT actor_id, message, type FROM stream WHERE source_id = me() LIMIT 10",
                                 "SELECT uid,name FROM user WHERE uid IN (SELECT actor_id FROM #query0)"};
        private static IDictionary<string, string> m_UidToName;

        private string m_WallPost;
        private int m_Type;        // This determines what kind of wall post it is..whether a post by someone, a status update by the user or an event created by the user etc
        private string m_SenderId;
        private string m_SenderName;
        private BitmapImage m_SenderImage;
        private static int m_MaxImageWidth = 64;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="resource">Name value dictionary.</param>
        public WallPostDataResources(IDictionary<string, object> resource)
        {
            Debug.Assert(resource != null);

            m_SenderId = resource["actor_id"] as string;
            Debug.Assert(m_SenderId != null);

            m_SenderName = m_UidToName[m_SenderId];
            Debug.Assert(m_SenderName != null);

            m_WallPost = resource["message"] as string;
            Debug.Assert(m_WallPost != null);

            var typeStr = resource["type"] as string;
            m_Type = -1;
            if (typeStr != null)
            {
                m_Type = int.Parse(typeStr);
            }

            // TODO: Use a dispatcher to get the thumbnails.
            Uri thumbnailUri = new Uri(string.Format("https://graph.facebook.com/{0}/picture", m_SenderId)); // Uri to the profile pic of the user with uid = m_SenderId
            m_SenderImage = new BitmapImage(thumbnailUri);
        }

        public static List<WallPostDataResources> createDataResources(IList<object> res)
        {
            List<WallPostDataResources> list = new List<WallPostDataResources>();
            
            // Create the UID to Names mapping
            if (m_UidToName == null)
            {
                m_UidToName = new Dictionary<string, string>();
            }
            m_UidToName.Clear();

            // query1 contains the uid to name objects
            Debug.Assert(res.Count == 2);
            var query1Res = res[1] as IDictionary<string, object>;
            Debug.Assert(query1Res != null);
            Debug.Assert((query1Res["name"] as string) == "query1");
            var idNameDictList = query1Res["fql_result_set"] as IList<object>;
            Debug.Assert(idNameDictList != null);

            foreach (object idNameDictObj in idNameDictList)
            {
                var idNameDict = idNameDictObj as IDictionary<string, object>;
                m_UidToName.Add((idNameDict["uid"] as string), (idNameDict["name"] as string));
            }

            // parse query0
            var query0Res = res[0] as IDictionary<string, object>;
            Debug.Assert(query0Res != null);
            Debug.Assert((query0Res["name"] as string) == "query0");
            var fbInfoList = query0Res["fql_result_set"] as IList<object>;
            Debug.Assert(fbInfoList != null);

            foreach (object obj in fbInfoList)
            {
                WallPostDataResources resources = new WallPostDataResources(obj as IDictionary<string, object>);
                list.Add(resources);
            }
            return list;
        }

        public static string[] FqlQuery
        {
            get { return m_FqlQuery; }
        }

        public string WallPost
        {
            get
            {
                return m_WallPost;
            }
        }

        public string SenderName
        {
          get
            {
               string tag ="";                          
               if(m_Type == 12)
                {
                   tag="created event";
                }
                else if(m_Type == 46)
                {
                   tag="updated status";
                }
                else if(m_Type == 56)
                {
                    tag="posted";
                }            
                return m_SenderName + "  " + tag ;
            }
        }

        public BitmapImage SenderImage
        {
            get
            {
                return m_SenderImage;
            }
        }

        public int MaxImageWidth
        {
            get
            {
                return m_MaxImageWidth;
            }
        }
    }
}
